{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "import numpy as np\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recall(rs, N=10): \n",
    "    \"\"\"\n",
    "    例子：\n",
    "    >>> rs = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]\n",
    "    >>> recall(rs, N=1)\n",
    "    0.333333\n",
    "    >>> recall(rs, N=2)\n",
    "    >>> 0.6666667\n",
    "    >>> recall(rs, N=3)\n",
    "    >>> 1.0\n",
    "    \"\"\"\n",
    "    \n",
    "    recall_flags = [np.sum(r[0:N]) for r in rs] \n",
    "                                                \n",
    "    return np.mean(recall_flags) \n",
    "                                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "text2similar = {}\n",
    "\n",
    "similar_text_pair = \"recall_dataset/dev.csv\"\n",
    "\n",
    "with open(similar_text_pair, \"r\", encoding=\"utf-8\") as f: \n",
    "    for line in f:\n",
    "        text, similar_text = line.rstrip().split(\"\\t\")\n",
    "        text2similar[text] = similar_text "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs = [] \n",
    "\n",
    "recall_result_file = \"recall_result_file/recall_result.txt\"\n",
    "recall_num = 50 \n",
    "\n",
    "with open(recall_result_file, \"r\", encoding=\"utf-8\") as f: \n",
    "    relevance_labels = []\n",
    "    for index, line in enumerate(f): \n",
    "        if index % recall_num == 0 and index != 0: \n",
    "            rs.append(relevance_labels)\n",
    "            relevance_labels = [] \n",
    "\n",
    "        query, recalled_text, cosine_sim = line.rstrip().split(\"\\t\") \n",
    "\n",
    "        if text2similar[query] == recalled_text: \n",
    "            relevance_labels.append(1) \n",
    "        else:\n",
    "            relevance_labels.append(0)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "recalls = [1, 5, 10, 20, 50] \n",
    "recall_N = [] \n",
    "\n",
    "for topN in recalls:\n",
    "    R = round(100 * recall(rs, N=topN), 3) \n",
    "    recall_N.append(R) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_tsv_file = \"recall_result_file/result.tsv\"\n",
    "\n",
    "with open(result_tsv_file, \"w\", encoding=\"utf-8\") as f: \n",
    "    res = []\n",
    "\n",
    "    for i in range(len(recalls)): \n",
    "        N = recalls[i] \n",
    "        recall_val = recall_N[i] \n",
    "        print(\"recall@{}={}\".format(N, recall_val)) \n",
    "        res.append(str(recall_val)) \n",
    "    \n",
    "    f.write(\"\\t\".join(res) + \"\\n\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "search",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
